'use strict';

/*-------------------------------------------------------------
       _                         
 _   _(_) __ _ _ __ _____      __
| | | | |/ _` | '__/ _ \ \ /\ / /
| |_| | | (_| | | | (_) \ V  V / 
\__ , |_|\__, |_|  \___/ \_/\_/  
 |___/   |___/ (C)2023 shhoo.cn

 1、查询可入网设备
 2、设置入网设备名称及路数
-------------------------------------------------------------*/
import moment from 'moment';
moment.locale('zh-cn');
import YAML from 'yamljs';
const Settings = YAML.load('settings.yaml');
const TCP_COM_CTRL = Settings.TCP_COM_CTRL;
const LORA_MODBUSTCP = Settings.LORA_MODBUSTCP;
import Curd from '../func/curd'
import { RedisGatewayClass } from "../lib/redis_gateway";
import Mysql from "../lib/mysqlUtil";
import { LoRaComCtrlClass } from "../device/loRaComCtrl"
import { TcpComCtrlClass } from "../device/tcpComCtrl"
interface LoRaQueryApplayDeviceInfo {
    addr: string,
    DONum: number,
    DINum: number,
    AINum: number,
    glsnr: number,
    grssi: number,
    lsnr: number,
    rssi: number,
    swVer: string,
    hwVer: string
}
interface TcpQueryApplayDeviceInfo {
    host: string,
    DONum: number,
    DINum: number,
    AINum: number,
    glsnr: number,
    grssi: number,
    lsnr: number,
    rssi: number,
    swVer: string,
    hwVer: string
}
function Parser(RedisGateway: RedisGatewayClass, LoRaComCtrl: LoRaComCtrlClass, TcpComCtrl: TcpComCtrlClass, confTyp: string, method: string, qryId: string, para: any | any[]) {
    switch (method) {
        case "QUERY_APPLAY":
            let addr = para.addr;
            if (addr != '00000000') {
                LoRaComCtrl.QueryApplayDevice(addr, 1, 1500, 2, true, function (err: string | null, data: LoRaQueryApplayDeviceInfo | null) {
                    if (data) {
                        let addr = data.addr;
                        let DONum = data.DONum;
                        let DINum = data.DINum;
                        let AINum = data.AINum;
                        let glsnr = data.glsnr;
                        let grssi = data.grssi;
                        let lsnr = data.lsnr;
                        let rssi = data.rssi;
                        let swVer = data.swVer;
                        let hwVer = data.hwVer;
                        for (let i = 0; i < DONum; i++) {
                            Curd.AddNewActuator('DO', addr, 0, i, 'LoRa_ComCtrl', glsnr, grssi, lsnr, rssi, 'G', swVer, hwVer).then(() => {

                            }).catch((err) => {
                                console.log(err)
                            });
                        }
                        for (let i = 0; i < DINum; i++) {
                            Curd.AddNewActuator('DI', addr, 0, i, 'LoRa_ComCtrl', glsnr, grssi, lsnr, rssi, 'G', swVer, hwVer).then(() => {

                            }).catch((err) => {
                                console.log(err)
                            });
                        }
                        Curd.AddNewActuator('AI', addr, 0, AINum, 'LoRa_ComCtrl', glsnr, grssi, lsnr, rssi, 'G', swVer, hwVer).then(() => {

                        }).catch((err) => {
                            console.log(err)
                        });

                    }
                })
            } else {
                for (let i = 0; i < TCP_COM_CTRL.enable.length; i++) {
                    if (TCP_COM_CTRL.enable[i]) {
                        const host = TCP_COM_CTRL.host[i];
                        TcpComCtrl.QueryApplayDevice(host, 1, 1500, 2, true, function (err: string | null, data: TcpQueryApplayDeviceInfo | null) {
                            if (data) {
                                console.log(data)
                                let addr = data.host;
                                let DONum = data.DONum;
                                let DINum = data.DINum;
                                let AINum = data.AINum;
                                let glsnr = data.glsnr;
                                let grssi = data.grssi;
                                let lsnr = data.lsnr;
                                let rssi = data.rssi;
                                let swVer = data.swVer;
                                let hwVer = data.hwVer;
                                for (let i = 0; i < DONum; i++) {
                                    Curd.AddNewActuator('DO', addr, 0, i, 'Tcp_ComCtrl', glsnr, grssi, lsnr, rssi, 'G', swVer, hwVer).then(() => {

                                    }).catch((err) => {
                                        console.log(err)
                                    });
                                }
                                for (let i = 0; i < DINum; i++) {
                                    Curd.AddNewActuator('DI', addr, 0, i, 'Tcp_ComCtrl', glsnr, grssi, lsnr, rssi, 'G', swVer, hwVer).then(() => {

                                    }).catch((err) => {
                                        console.log(err)
                                    });
                                }
                                Curd.AddNewActuator('AI', addr, 0, AINum, 'Tcp_ComCtrl', glsnr, grssi, lsnr, rssi, 'G', swVer, hwVer).then(() => {

                                }).catch((err) => {
                                    console.log(err)
                                });

                            }
                        })
                    }

                }
                console.log('直接添加设备')
                if (LORA_MODBUSTCP.enable) {
                    let addr = LORA_MODBUSTCP.path;
                    if (LORA_MODBUSTCP.type == 0) {
                        addr = LORA_MODBUSTCP.host;
                    }
                    for (let i = 0; i < LORA_MODBUSTCP.id.length; i++) {
                        let DONum = LORA_MODBUSTCP.DONum[i];
                        let DINum = LORA_MODBUSTCP.DINum[i];
                        let AINum = LORA_MODBUSTCP.AINum[i];
                        let glsnr = 0;
                        let grssi = 0;
                        let lsnr = 0;
                        let rssi = 0;
                        let swVer = "V0";
                        let hwVer = "V0";
                        for (let j = 0; j < DONum; j++) {
                            Curd.AddNewActuator('DO', addr, LORA_MODBUSTCP.id[i], j, 'LORA_MODBUSTCP', glsnr, grssi, lsnr, rssi, 'G', swVer, hwVer).then(() => {

                            }).catch((err) => {
                                console.log(err)
                            });
                        }
                        for (let j = 0; j < DINum; j++) {
                            Curd.AddNewActuator('DI', addr, LORA_MODBUSTCP.id[i], j, 'LORA_MODBUSTCP', glsnr, grssi, lsnr, rssi, 'G', swVer, hwVer).then(() => {

                            }).catch((err) => {
                                console.log(err)
                            });
                        }
                        Curd.AddNewActuator('AI', addr, LORA_MODBUSTCP.id[i], AINum, 'LORA_MODBUSTCP', glsnr, grssi, lsnr, rssi, 'G', swVer, hwVer).then(() => {

                        }).catch((err) => {
                            console.log(err)
                        });
                    }

                }

            }

            break;
        case "APPLAY_ACTUATORS_INFO":
            Curd.QueryAllNewActuatorsInfo().then((infos) => {
                RedisGateway.pubActuatorCmdAck('COM_CTRL', "APPLAY_ACTUATORS_INFO", qryId, 'success', '查询所有控制器信息成功', infos);
            }).catch((err) => {
                RedisGateway.pubActuatorCmdAck('COM_CTRL', "APPLAY_ACTUATORS_INFO", qryId, 'failure', '查询所有控制器信息成功失败', err);
                console.log(err)
            });
            break;
        case "BIND_ROLLER":
            if (Array.isArray(para) == true && para.length > 0) {
                for (let i = 0; i < para.length; i++) {
                    let infos = para[i];
                    let name = infos.name;//卷膜器名
                    let zoneId = infos.zoneId;//区域ID
                    let addr = infos.addr;//地址
                    let openChName = infos.openChName;//开启通道名称
                    let closeChName = infos.closeChName;//关闭通道名称
                    let openCh = parseInt(openChName.split("_")[3]);
                    let closeCh = parseInt(closeChName.split("_")[3]);
                    let groupIdArray = infos.groupIdArray;//组ID数组
                    let openTravel = infos.openTravel;//开启行程时间数组
                    let closeTravel = infos.closeTravel;//关闭行程时间数组
                    let forceOpenTime = infos.forceOpenTime;//强制全开时长
                    let forceCloseTime = infos.forceCloseTime;//强制全关时长
                    let openCorrection = infos.openCorrection;//开启校正偏差（每次）
                    let closeCorrection = infos.closeCorrection;//关闭校正偏差（每次）
                    let scrapTimeSec = infos.scrapTimeSec;//搭边时间
                    let errCorrectionNumber = infos.errCorrectionNumber;//误差校正总次数
                    let isForceOpen = infos.isForceOpen;//100%是否开到限位
                    let isUsedAI = infos.isUsedAI;
                    let AIChName = infos.AIChName;
                    let settings = {
                        "openChName": openChName,
                        "openTravel": openTravel,
                        "forceOpenTime": forceOpenTime,
                        "openCorrection": openCorrection,
                        "closeChName": closeChName,
                        "closeTravel": closeTravel,
                        "forceCloseTime": forceCloseTime,
                        "closeCorrection": closeCorrection,
                        "scrapTimeSec": scrapTimeSec,
                        "errCorrectionNumber": errCorrectionNumber,
                        "isForceOpen": isForceOpen,
                        "isUsedAI": isUsedAI,
                        "AIChName": AIChName,
                        "errCorrectionCnt": 0
                    }
                    Mysql.DeletParamsFromTable(`t_actuator`, `f_actuator_addr='${addr}' AND (f_channel=${openCh} OR f_channel=${closeCh}) AND f_actuator_type_id ="B001"`).then(() => {
                        let slaverSn = `B001${moment().format("YYMMDDHHmmss")}${openCh.toString().padStart(2, '0')}`;
                        Mysql.FieldIsExists(`t_actuator`, 'f_actuator_sn', `'${slaverSn}'`).then((sta) => {
                            if (sta == true) {
                                console.log(`${new Date().toLocaleString()}【actuatorCmd.ts/<Parser>】${new Date().toLocaleString()}`, `SN为${slaverSn}的通用控制器子设备已存在！！`);
                            } else {
                                let f_actuator_alias = name;
                                let f_actuator_type = 'LoRa_Roller'; //"执行器类型",
                                let f_actuator_type_alias = '后端版卷膜电机'; //"执行器类型别名",
                                let f_actuator_type_id = 'B001'; //"执行器类型id",
                                let f_actuator_type_mac = null; // "执行器MAC",
                                let f_actuator_type_media = 'LoRa'; // "执行器数据传输媒介",
                                let f_actuator_status = '出厂模式'; // "执行器当前状态",
                                let f_actuator_addr = addr; // "执行器地址",
                                let f_actuator_interval = null; //
                                let f_actuator_allocate = '005DC00014000003F1115300000000'; //
                                let f_channel = openCh; //
                                let f_zone_id = zoneId; //
                                let f_actuator_settings = 'LoRa_Roller'; // "设置",
                                let f_actuator_power_source = '220000'; // "执行器供电源",
                                let f_actuator_type_tag = `OPEN:${openChName},OPEN:${closeChName}`; // "执行器tag",
                                let f_actuator_csq = -78; //"csq",
                                let f_csq_baseline = null; //"csq",
                                let f_last_value = '0'; // "执行器的最近数据（JSON）格式",
                                let f_actuator_vol = null; // "执行器电量",
                                let f_actuator_rssi = 8; // "执行器相对信号强度"
                                let f_pre_allocate1 = `{"openTimeSec":220,"closeTimeSec":220}`; // "预设值1",
                                let f_pre_allocate2 = null; // "预设值2",
                                let f_pre_allocate3 = JSON.stringify(settings);
                                let f_vol_baseline = null; //"电压基准",
                                let f_rssi_baseline = null; //"rssi基准线"
                                let f_remark = null; //"备注",
                                let f_enable = 1; //"是否启用",
                                let f_bindgroup_enable = 1; //"是否可绑定",
                                let f_actuator_edit_time = moment().format('YYYY-MM-DD HH:mm:ss'); //"修改时间",
                                // let f_interface_settings = `{"channel":${i},"isReverse":0,"AI":${i + 1},"lowAI":620,"highAI":1}`;
                                let paramKeys = 'f_actuator_sn,f_actuator_alias,f_actuator_type,f_actuator_type_id,f_actuator_type_alias,f_actuator_addr,f_actuator_group_id_array,f_actuator_interval,f_actuator_allocate,f_channel,f_zone_id,f_actuator_status,f_last_value,f_actuator_settings,f_actuator_type_mac,f_actuator_type_media,f_actuator_power_source,f_actuator_type_tag,f_actuator_vol,f_vol_baseline,f_actuator_rssi,f_rssi_baseline,f_actuator_csq,f_csq_baseline,f_pre_allocate1,f_pre_allocate2,f_pre_allocate3,f_remark,f_enable,f_bindgroup_enable,f_actuator_edit_time';
                                let paramDatas = `${(slaverSn == null) ? null : `'${slaverSn}'`},${(f_actuator_alias == null) ? null : `'${f_actuator_alias}'`},${(f_actuator_type == null) ? null : `'${f_actuator_type}'`},${(f_actuator_type_id == null) ? null : `'${f_actuator_type_id}'`},${(f_actuator_type_alias == null) ? null : `'${f_actuator_type_alias}'`}, ${(f_actuator_addr == null) ? null : `'${f_actuator_addr}'`},${(groupIdArray == null) ? null : `'${groupIdArray}'`},${f_actuator_interval},${(f_actuator_allocate == null) ? null : `'${f_actuator_allocate}'`},${f_channel},${f_zone_id},${(f_actuator_status == null) ? null : `'${f_actuator_status}'`},${(f_last_value == null) ? null : `'${f_last_value}'`},${(f_actuator_settings == null) ? null : `'${f_actuator_settings}'`},${(f_actuator_type_mac == null) ? null : `'${f_actuator_type_mac}'`},${(f_actuator_type_media == null) ? null : `'${f_actuator_type_media}'`},${(f_actuator_power_source == null) ? null : `'${f_actuator_power_source}'`},${(f_actuator_type_tag == null) ? null : `'${f_actuator_type_tag}'`},${f_actuator_vol},${f_vol_baseline},${f_actuator_rssi},${f_rssi_baseline},${f_actuator_csq},${f_csq_baseline},${(f_pre_allocate1 == null) ? null : `'${f_pre_allocate1}'`},${(f_pre_allocate2 == null) ? null : `'${f_pre_allocate2}'`},${(f_pre_allocate3 == null) ? null : `'${f_pre_allocate3}'`},${(f_remark == null) ? null : `'${f_remark}'`},${f_enable == null ? null : f_enable},${f_bindgroup_enable == null ? null : f_bindgroup_enable},${(f_actuator_edit_time == null) ? null : `'${f_actuator_edit_time}'`}`;
                                Mysql.InsertParamsIntoTable(`t_actuator`, paramKeys, paramDatas).then(() => {
                                    console.log(`${new Date().toLocaleString()}【actuatorCmd.ts/<Parser>】${new Date().toLocaleString()}`, '[t_actuator--ADD] ADD Com_Ctrl Slaver information successfully!!!');
                                    RedisGateway.pubSystemCmdAck('COM_CTRL', 'BIND_ROLLER', qryId, 'success', `添加通用控制器子设备信息成功`, para);

                                }).catch((err) => {
                                    console.log(`${new Date().toLocaleString()}【actuatorCmd.ts/<Parser>】${new Date().toLocaleString()}`, '[t_actuator--ADD]', err.toString());
                                    RedisGateway.pubSystemCmdAck('COM_CTRL', 'BIND_ROLLER', qryId, 'failure', `添加通用控制器子设备信息失败`, err.toString());
                                });
                            }
                        })
                    })
                }

            }

            break;
        case "BIND_SCREEN":
            if (Array.isArray(para) == true && para.length > 0) {
                for (let i = 0; i < para.length; i++) {
                    let infos = para[i];
                    let name = infos.name;//卷膜器名
                    let zoneId = infos.zoneId;//区域ID
                    let addr = infos.addr;//地址
                    let openChName = infos.openChName;//开启通道名称
                    let closeChName = infos.closeChName;//关闭通道名称
                    let openCh = parseInt(openChName.split("_")[3]);
                    let closeCh = parseInt(closeChName.split("_")[3]);
                    let groupIdArray = infos.groupIdArray;//组ID数组
                    let openTravel = infos.openTravel;//开启行程时间数组
                    let closeTravel = infos.closeTravel;//关闭行程时间数组
                    let forceOpenTime = infos.forceOpenTime;//强制全开时长
                    let forceCloseTime = infos.forceCloseTime;//强制全关时长
                    let openCorrection = infos.openCorrection;//开启校正偏差（每次）
                    let closeCorrection = infos.closeCorrection;//关闭校正偏差（每次）
                    let scrapTimeSec = infos.scrapTimeSec;//搭边时间
                    let errCorrectionNumber = infos.errCorrectionNumber;//误差校正总次数
                    let isForceOpen = infos.isForceOpen;//100%是否开到限位
                    let isUsedAI = infos.isUsedAI;
                    let AIChName = infos.AIChName;
                    let settings = {
                        "openChName": openChName,
                        "openTravel": openTravel,
                        "forceOpenTime": forceOpenTime,
                        "openCorrection": openCorrection,
                        "closeChName": closeChName,
                        "closeTravel": closeTravel,
                        "forceCloseTime": forceCloseTime,
                        "closeCorrection": closeCorrection,
                        "scrapTimeSec": scrapTimeSec,
                        "errCorrectionNumber": errCorrectionNumber,
                        "isForceOpen": isForceOpen,
                        "isUsedAI": isUsedAI,
                        "AIChName": AIChName,
                        "errCorrectionCnt": 0
                    }
                    Mysql.DeletParamsFromTable(`t_actuator`, `f_actuator_addr='${addr}' AND (f_channel=${openCh} OR f_channel=${closeCh})  AND f_actuator_type_id ="B002"`).then(() => {
                        let slaverSn = `B002${moment().format("YYYYMMDDHHmm")}${openCh}`.padEnd(18, '0');
                        Mysql.FieldIsExists(`t_actuator`, 'f_actuator_sn', `'${slaverSn}'`).then((sta) => {
                            if (sta == true) {
                                console.log(`${new Date().toLocaleString()}【actuatorCmd.ts/<Parser>】${new Date().toLocaleString()}`, `SN为${slaverSn}的通用控制器子设备已存在！！`);
                            } else {
                                let f_actuator_alias = name;
                                let f_actuator_type = 'LoRa_Screen'; //"执行器类型",
                                let f_actuator_type_alias = '后端版拉幕电机'; //"执行器类型别名",
                                let f_actuator_type_id = 'B002'; //"执行器类型id",
                                let f_actuator_type_mac = null; // "执行器MAC",
                                let f_actuator_type_media = 'LoRa'; // "执行器数据传输媒介",
                                let f_actuator_status = '出厂模式'; // "执行器当前状态",
                                let f_actuator_addr = addr; // "执行器地址",
                                let f_actuator_interval = null; //
                                let f_actuator_allocate = '005DC00014000003F1115300000000'; //
                                let f_channel = openCh; //
                                let f_zone_id = zoneId; //
                                let f_actuator_settings = 'LoRa_Screen'; // "设置",
                                let f_actuator_power_source = '380000'; // "执行器供电源",
                                let f_actuator_type_tag = `OPEN:${openChName},OPEN:${closeChName}`; // "执行器tag",
                                let f_actuator_csq = null; //"csq",
                                let f_csq_baseline = -78; //"csq",
                                let f_last_value = '0'; // "执行器的最近数据（JSON）格式",
                                let f_actuator_vol = null; // "执行器电量",
                                let f_actuator_rssi = null; // "执行器相对信号强度"
                                let f_pre_allocate1 = null; // "预设值1",
                                let f_pre_allocate2 = null; // "预设值2",
                                let f_pre_allocate3 = JSON.stringify(settings);
                                let f_vol_baseline = null; //"电压基准",
                                let f_rssi_baseline = 8; //"rssi基准线"
                                let f_remark = null; //"备注",
                                let f_enable = 1; //"是否启用",
                                let f_bindgroup_enable = 1; //"是否可绑定",
                                let f_actuator_edit_time = moment().format('YYYY-MM-DD HH:mm:ss'); //"修改时间",
                                // let f_interface_settings = `{"channel":${i},"isReverse":0,"AI":${i + 1},"lowAI":620,"highAI":1}`;
                                let paramKeys = 'f_actuator_sn,f_actuator_alias,f_actuator_type,f_actuator_type_id,f_actuator_type_alias,f_actuator_addr,f_actuator_group_id_array,f_actuator_interval,f_actuator_allocate,f_channel,f_zone_id,f_actuator_status,f_last_value,f_actuator_settings,f_actuator_type_mac,f_actuator_type_media,f_actuator_power_source,f_actuator_type_tag,f_actuator_vol,f_vol_baseline,f_actuator_rssi,f_rssi_baseline,f_actuator_csq,f_csq_baseline,f_pre_allocate1,f_pre_allocate2,f_pre_allocate3,f_remark,f_enable,f_bindgroup_enable,f_actuator_edit_time';
                                let paramDatas = `${(slaverSn == null) ? null : `'${slaverSn}'`},${(f_actuator_alias == null) ? null : `'${f_actuator_alias}'`},${(f_actuator_type == null) ? null : `'${f_actuator_type}'`},${(f_actuator_type_id == null) ? null : `'${f_actuator_type_id}'`},${(f_actuator_type_alias == null) ? null : `'${f_actuator_type_alias}'`}, ${(f_actuator_addr == null) ? null : `'${f_actuator_addr}'`},${(groupIdArray == null) ? null : `'${groupIdArray}'`},${f_actuator_interval},${(f_actuator_allocate == null) ? null : `'${f_actuator_allocate}'`},${f_channel},${f_zone_id},${(f_actuator_status == null) ? null : `'${f_actuator_status}'`},${(f_last_value == null) ? null : `'${f_last_value}'`},${(f_actuator_settings == null) ? null : `'${f_actuator_settings}'`},${(f_actuator_type_mac == null) ? null : `'${f_actuator_type_mac}'`},${(f_actuator_type_media == null) ? null : `'${f_actuator_type_media}'`},${(f_actuator_power_source == null) ? null : `'${f_actuator_power_source}'`},${(f_actuator_type_tag == null) ? null : `'${f_actuator_type_tag}'`},${f_actuator_vol},${f_vol_baseline},${f_actuator_rssi},${f_rssi_baseline},${f_actuator_csq},${f_csq_baseline},${(f_pre_allocate1 == null) ? null : `'${f_pre_allocate1}'`},${(f_pre_allocate2 == null) ? null : `'${f_pre_allocate2}'`},${(f_pre_allocate3 == null) ? null : `'${f_pre_allocate3}'`},${(f_remark == null) ? null : `'${f_remark}'`},${f_enable == null ? null : f_enable},${f_bindgroup_enable == null ? null : f_bindgroup_enable},${(f_actuator_edit_time == null) ? null : `'${f_actuator_edit_time}'`}`;
                                Mysql.InsertParamsIntoTable(`t_actuator`, paramKeys, paramDatas).then(() => {
                                    console.log(`${new Date().toLocaleString()}【actuatorCmd.ts/<Parser>】${new Date().toLocaleString()}`, '[t_actuator--ADD] ADD Com_Ctrl Slaver information successfully!!!');
                                    RedisGateway.pubSystemCmdAck('COM_CTRL', 'BIND_SCREEN', qryId, 'success', `添加通用控制器子设备信息成功`, para);

                                }).catch((err) => {
                                    console.log(`${new Date().toLocaleString()}【actuatorCmd.ts/<Parser>】${new Date().toLocaleString()}`, '[t_actuator--ADD]', err.toString());
                                    RedisGateway.pubSystemCmdAck('COM_CTRL', 'BIND_SCREEN', qryId, 'failure', `添加通用控制器子设备信息失败`, err.toString());
                                });
                            }
                        })
                    })
                }

            }

            break;
        case "BIND_FAN":
            let masterName = para.masterName;
            let nameArray = para.nameArray;//卷膜器名
            let openChNameArray = para.openChNameArray;//开启通道名称
            let isUsedAI = para.isUsedAI;//是否启用AI
            let AINameArray = para.AINameArray;
            let settings = {
                openChNameArray: openChNameArray,
                isUsedAI: isUsedAI,
                AINameArray: AINameArray,
            }
            let fan_addr = para.addr;
            let fan_zoneId = para.zoneId;
            let channelCount = nameArray.length;
            let last_value = new Array(nameArray.length).fill(0);
            Mysql.DeletParamsFromTable(`t_actuator`, `f_actuator_addr='${fan_addr}' AND f_actuator_type_id ="B003"`).then(() => {
                let slaverSn = `B003${moment().format("YYYYMMDDHHmm")}${channelCount}`.padEnd(18, '0');
                Mysql.FieldIsExists(`t_actuator`, 'f_actuator_sn', `'${slaverSn}'`).then((sta) => {
                    if (sta == true) {
                        console.log(`${new Date().toLocaleString()}【actuatorCmd.ts/<Parser>】${new Date().toLocaleString()}`, `SN为${slaverSn}的通用控制器子设备已存在！！`);
                    } else {
                        let f_actuator_alias = masterName;
                        let f_actuator_type = 'LoRa_Fan'; //"执行器类型",
                        let f_actuator_type_alias = '后端版风机'; //"执行器类型别名",
                        let f_actuator_type_id = 'B003'; //"执行器类型id",
                        let f_actuator_type_mac = null; // "执行器MAC",
                        let f_actuator_type_media = 'LoRa'; // "执行器数据传输媒介",
                        let f_actuator_status = '出厂模式'; // "执行器当前状态",
                        let f_actuator_addr = fan_addr; // "执行器地址",
                        let f_actuator_interval = null; //
                        let f_actuator_allocate = null; //
                        let f_channel = channelCount; //
                        let f_zone_id = fan_zoneId; //
                        let f_actuator_settings = 'LoRa_Fan'; // "设置",
                        let f_actuator_power_source = '380000'; // "执行器供电源",
                        let f_actuator_type_tag = null; // "执行器tag",
                        let f_actuator_csq = -78; //"csq",
                        let f_csq_baseline = null; //"csq",
                        let f_last_value = JSON.stringify(last_value); // "执行器的最近数据（JSON）格式",
                        let f_actuator_vol = null; // "执行器电量",
                        let f_actuator_rssi = 8; // "执行器相对信号强度"
                        let f_pre_allocate1 = JSON.stringify(nameArray); // "预设值1",
                        let f_pre_allocate2 = null; // "预设值2",
                        let f_pre_allocate3 = JSON.stringify(settings);
                        let f_vol_baseline = null; //"电压基准",
                        let f_rssi_baseline = null; //"rssi基准线"
                        let f_remark = null; //"备注",
                        let f_enable = 1; //"是否启用",
                        let f_bindgroup_enable = 1; //"是否可绑定",
                        let f_actuator_edit_time = moment().format('YYYY-MM-DD HH:mm:ss'); //"修改时间",
                        // let f_interface_settings = `{"channel":${i},"isReverse":0,"AI":${i + 1},"lowAI":620,"highAI":1}`;
                        let paramKeys = 'f_actuator_sn,f_actuator_alias,f_actuator_type,f_actuator_type_id,f_actuator_type_alias,f_actuator_addr,f_actuator_group_id_array,f_actuator_interval,f_actuator_allocate,f_channel,f_zone_id,f_actuator_status,f_last_value,f_actuator_settings,f_actuator_type_mac,f_actuator_type_media,f_actuator_power_source,f_actuator_type_tag,f_actuator_vol,f_vol_baseline,f_actuator_rssi,f_rssi_baseline,f_actuator_csq,f_csq_baseline,f_pre_allocate1,f_pre_allocate2,f_pre_allocate3,f_remark,f_enable,f_bindgroup_enable,f_actuator_edit_time';
                        let paramDatas = `${(slaverSn == null) ? null : `'${slaverSn}'`},${(f_actuator_alias == null) ? null : `'${f_actuator_alias}'`},${(f_actuator_type == null) ? null : `'${f_actuator_type}'`},${(f_actuator_type_id == null) ? null : `'${f_actuator_type_id}'`},${(f_actuator_type_alias == null) ? null : `'${f_actuator_type_alias}'`}, ${(f_actuator_addr == null) ? null : `'${f_actuator_addr}'`},${null},${f_actuator_interval},${(f_actuator_allocate == null) ? null : `'${f_actuator_allocate}'`},${f_channel},${f_zone_id},${(f_actuator_status == null) ? null : `'${f_actuator_status}'`},${(f_last_value == null) ? null : `'${f_last_value}'`},${(f_actuator_settings == null) ? null : `'${f_actuator_settings}'`},${(f_actuator_type_mac == null) ? null : `'${f_actuator_type_mac}'`},${(f_actuator_type_media == null) ? null : `'${f_actuator_type_media}'`},${(f_actuator_power_source == null) ? null : `'${f_actuator_power_source}'`},${(f_actuator_type_tag == null) ? null : `'${f_actuator_type_tag}'`},${f_actuator_vol},${f_vol_baseline},${f_actuator_rssi},${f_rssi_baseline},${f_actuator_csq},${f_csq_baseline},${(f_pre_allocate1 == null) ? null : `'${f_pre_allocate1}'`},${(f_pre_allocate2 == null) ? null : `'${f_pre_allocate2}'`},${(f_pre_allocate3 == null) ? null : `'${f_pre_allocate3}'`},${(f_remark == null) ? null : `'${f_remark}'`},${f_enable == null ? null : f_enable},${f_bindgroup_enable == null ? null : f_bindgroup_enable},${(f_actuator_edit_time == null) ? null : `'${f_actuator_edit_time}'`}`;
                        Mysql.InsertParamsIntoTable(`t_actuator`, paramKeys, paramDatas).then(() => {
                            console.log(`${new Date().toLocaleString()}【actuatorCmd.ts/<Parser>】${new Date().toLocaleString()}`, '[t_actuator--ADD] ADD Com_Ctrl Slaver information successfully!!!');
                            RedisGateway.pubSystemCmdAck('COM_CTRL', 'BIND_FAN', qryId, 'success', `添加通用控制器子设备信息成功`, para);

                        }).catch((err) => {
                            console.log(`${new Date().toLocaleString()}【actuatorCmd.ts/<Parser>】${new Date().toLocaleString()}`, '[t_actuator--ADD]', err.toString());
                            RedisGateway.pubSystemCmdAck('COM_CTRL', 'BIND_FAN', qryId, 'failure', `添加通用控制器子设备信息失败`, err.toString());
                        });

                    }
                })
            })
            break;
        default:
            break;
    }
}

export = {
    Parser: Parser
}